package NewCoder;

public class BM38 {
    int lca;
    public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
        // write code here
        if(root==null) return -1;
        lowest(root,o1,o2);
        return lca;
    }

    private boolean lowest(TreeNode root,int p,int q){
        if(root==null) return false;
        int a=lowest(root.left,p,q)?1:0;
        int b=lowest(root.right,p,q)?1:0;
        int mid=(root.val==p || root.val==q)?1:0;
        if(a+b+mid==2){
            lca=root.val;
            return true;
        }
        return (a+b+mid)>0;
    }
}
